package main

import (
	"fmt"
	"net/http"
	"strings"
)

// main
//
//	@Description: request
func main() {

	http.HandleFunc("/get", handleGetParam)
	http.HandleFunc("/post", handlePostParam)
	http.HandleFunc("/postOne", handlePostParamOne)
	http.HandleFunc("/postTwo", handlePostParamTwo)
	http.HandleFunc("/postThree", handlePostParamThree)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		fmt.Println("ListenAndServe: ", err)
		return
	}
}

// handleGetParam
//
//	@Description: 处理器函数 :http://localhost:8080/get?key1=123&key2=333
//	@param w
//	@param r
func handleGetParam(w http.ResponseWriter, r *http.Request) {
	//获取路径
	fmt.Println("获取路径相关---------------------------------")
	fmt.Println("Path:", r.URL.Path)         //Path: /hello
	fmt.Println("RawQuery:", r.URL.RawQuery) //RawQuery: username=zhagnsan&password=123

	fmt.Println("获取请求头---------------------------------")
	//获取请求头
	headers := r.Header
	for key, values := range headers {
		fmt.Printf("header: %s values:%v \n", key, values)
	}
	fmt.Println("获取某个头---------------------------------")
	//获取某个头
	encodings := r.Header["Accept-Encoding"]
	for _, value := range encodings {
		fmt.Printf("Accept-Encoding:%s \n", value)
	}
	encodingStrings := r.Header.Get("Accept-Encoding")
	fmt.Printf("Accept-Encoding:%s \n", encodingStrings)

	username := r.FormValue("username")
	password := r.FormValue("password")
	fmt.Fprintf(w, "username:%s  password:%s", username, password)
	//username:zhagnsan  password:123
}

func handlePostParam(w http.ResponseWriter, r *http.Request) {
	//获取长度
	length := r.ContentLength
	body := make([]byte, length)
	r.Body.Read(body)
	fmt.Fprintln(w, "请求体中的内容是：", string(body))
}

// handlePostParamOne
//
//	@Description:
//	注意 Content-Type = x-www-form-urlencoded
//
// URL编码（key=value&key2=value2）
// 仅文本
// GO的处理方式：r.ParseForm() + PostForm
//
//	@param w
//	@param r
func handlePostParamOne(w http.ResponseWriter, r *http.Request) {
	// 验证请求类型
	ct := r.Header.Get("Content-Type")
	if !strings.Contains(ct, "x-www-form-urlencoded") {
		http.Error(w, "需要表单类型请求", http.StatusBadRequest)
		return
	}
	err := r.ParseForm()
	if err != nil {
		fmt.Println("ParseForm err:", err)
		return
	}
	fmt.Println("请求体中的内容是：", r.Form)     //map[name:[zhangsan] password:[123456]]
	fmt.Println("请求体中的内容是：", r.PostForm) //map[name:[zhangsan] password:[123456]]
	fmt.Fprintln(w, "请求体中的内容是：", r.Form)
}

// handlePostParamTwo
//
//		@Description:
//		Content-Type = form-data
//		二进制分块传输（含边界符）
//		支持文件/二进制 + 文本混合
//	 GO的处理方式：r.ParseMultipartForm() + FormFile
//
//		@param w
//		@param r
func handlePostParamTwo(w http.ResponseWriter, r *http.Request) {
	// 验证请求类型
	err := r.ParseMultipartForm(32 << 20)
	if err != nil {
		fmt.Println("ParseForm err:", err)
		return
	}
	fmt.Println("请求体中的内容是：", r.Form)          //map[name:[zhangsan] password:[123456]]
	fmt.Println("请求体中的内容是：", r.PostForm)      //map[name:[zhangsan] password:[123456]]
	fmt.Println("请求体中的内容是：", r.MultipartForm) //&{map[name:[zhangsan] password:[123456]] map[]}
	fmt.Fprintln(w, "请求体中的内容是：", r.Form)
}

func handlePostParamThree(w http.ResponseWriter, r *http.Request) {
	//自动调用 r.ParseMultipartForm() 和 r.ParseForm()
	fmt.Println("username：", r.FormValue("username"))
	fmt.Println("password：", r.FormValue("password"))
	form := r.PostForm
	for k, v := range form {
		fmt.Println(k, v)
	}
	fmt.Fprintln(w, "请求体中的username：", r.FormValue("username"))
}
